/*
 * \brief  Pistachio pager support for Genode
 * \author Christian Helmuth
 * \date   2006-06-14
 */

/*
 * Copyright (C) 2006-2009 Christian Helmuth
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__BASE__IPC_PAGER_H_
#define _INCLUDE__BASE__IPC_PAGER_H_

#include <base/ipc.h>
#include <base/stdint.h>
#include <pistachio/kip.h>

namespace Pistachio {
#include <l4/types.h>
}

namespace Genode {

	class Fpage
	{
		private:

			union {
				Pistachio::L4_MapItem_t   _map_item;
				Pistachio::L4_GrantItem_t _grant_item;
			};

		public:

			/**
			 * Construct flexpage
			 */
			Fpage(Genode::addr_t snd_base, Genode::addr_t addr,
			      unsigned l2size = Pistachio::get_page_size_log2(),
			      bool rw = true, bool grant = false);

			/**
			 * Construct invalid flexpage
			 */
			Fpage();

			addr_t base() const { return Pistachio::L4_SndBase(_map_item); }

			Pistachio::L4_Fpage_t fpage() const {
				return Pistachio::L4_MapItemSndFpage(_map_item); }

			Pistachio::L4_MapItem_t map_item() const { return _map_item; };
	};


	/**
	 * Special paging server class
	 */
	class Ipc_pager : public Capability
	{
		private:

			bool                     _dw_select;

			Pistachio::L4_ThreadId_t _last;
			Pistachio::L4_Word_t     _flags;
			Genode::addr_t           _pf_addr;
			Genode::addr_t           _ip;

			Pistachio::L4_MapItem_t  _map_item;

		protected:

			/**
			 * Wait for short-message (register) IPC -- pagefault
			 */
			void _wait();

			/**
			 * Send short flex page and
			 * wait for next short-message (register) IPC -- pagefault
			 */
			void _reply_and_wait();

		public:

			/**
			 * Constructor
			 */
			Ipc_pager();

			/**
			 * Wait for a new page fault received as short message IPC
			 */
			void wait_for_page_fault();

			/**
			 * Reply current page-fault and wait for a new one
			 *
			 * Send short flex page and wait for next short-message (register)
			 * IPC -- pagefault
			 */
			void reply_and_wait_for_page_fault();

			/**
			 * Request instruction pointer of current page fault
			 */
			addr_t pf_ip() { return _ip; }

			/**
			 * Request fault address of current page fault
			 */
			addr_t pf_addr() { return _pf_addr & ~3; }

			/**
			 * Set parameters for next reply
			 */
			void set_reply_fpage(Fpage f) { _map_item = f.map_item(); }

			/**
			 * Set destination for next reply
			 */
			void set_reply_dst(Capability pager_object) {
				_last.raw = pager_object.local_name(); }

			/**
			 * Answer call without sending a flex-page mapping
			 *
			 * This function is used to acknowledge local calls from one of
			 * core's region-manager sessions.
			 */
			void acknowledge_wakeup();

			/** XXX DEBUG */
			Pistachio::L4_ThreadId_t last() const { return _last; }

			/**
			 * Return badge for faulting thread
			 *
			 * As L4v4 has no server-defined badges for page-fault messages,
			 * we interpret the sender ID as badge.
			 */
			unsigned long badge() const { return _last.raw; }

			bool write_pf() const { return (_flags & 2); }
	};
};

#endif /* _INCLUDE__BASE__IPC_PAGER_H_ */
